package com.kxscs.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.kxscs.R;
import com.kxscs.adapter.OrderAdapter;
import com.kxscs.bean.OrderBean;
import com.kxscs.constant.UrlConstant;
import com.kxscs.netBean.GeoUtil;
import com.kxscs.util.AppHttpUtils;
import com.kxscs.view.MySearchView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class OrderListActivity extends BaseActivity implements AdapterView.OnItemClickListener, MySearchView.OnTextChanged {
    public static final int UNDO = 0;// 0-待商务审批
    public static final int REFUSE = 1;// 1-商务审批通过
    public static final int PASS = 2;// 2-商务审批失败
    private int statue;
    private MySearchView searchView;
    private ListView listView;
    private OrderAdapter adapter;
    private List<OrderBean> orderList;
    private List<OrderBean> list = new ArrayList<>();

    @Override
    protected void setView() {
        setContentView(R.layout.activity_order_ticket);
    }

    @Override
    protected void initView(TextView leftView, TextView titleView, TextView rightView) {
        searchView = (MySearchView) findViewById(R.id.search_ticket);
        searchView.setHint(R.string.search_order_tip);
        searchView.setOnTextContentChange(this);
        listView = (ListView) findViewById(R.id.ticket_list);
        adapter = new OrderAdapter(this, list);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
    }
    @Override
    protected void initData(){
        statue = getIntent().getIntExtra(TYPE,0);
//        if(CacheDataConstant.orderList.get(getIntent().getIntExtra(TYPE,UNDO))==null){
//            CacheDataConstant.orderList.put(getIntent().getIntExtra(TYPE,UNDO),new ArrayList<OrderEntity>());
//        }
        showBufferDialog();
        AppHttpUtils.getUtils(UrlConstant.submitOrder, new Handler() {
            @Override
            public void handleMessage(Message msg) {
                try {
                    if(msg.obj!=null){
                        JSONObject object = new JSONObject(msg.obj.toString());
                        String json = object.getString("result");
                        if(json!=null)
                            orderList = new GeoUtil().deserializer(json, new TypeToken<List<OrderBean>>() {
                            }.getType());
                        if(orderList!=null&&orderList.size()>0){
                            for (int i = 0; i < orderList.size(); i++) {
                                if(orderList.get(i).getState()==statue){
                                    list.add(orderList.get(i));
                                }
                            }
                            adapter.notifyDataSetChanged();
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }finally {
                    if(list.size()==0){
                        emptyView.setImageResource(R.mipmap.no_msg);
                    }else {
                        bufferDialog.dismiss();
                    }
                }
            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        startActivityForResult(new Intent(this,OrderDetailActivity.class).
                putExtra(ID, list.get(position).orderno),LOCKCODE);
    }

    @Override
    public void onTextChangedListener(CharSequence charSequence, int i, int i1, int i2) {
        list.clear();
        for(OrderBean entity:orderList){
            if(entity.orderno.contains(charSequence.toString().trim())
                    ||entity.sc_name.contains(charSequence.toString().trim().toLowerCase())
                    ||entity.start_name.contains(charSequence.toString().trim().toLowerCase())
                    ||entity.des_name.contains(charSequence.toString().trim().toLowerCase())
                    ||entity.sc_name.contains(charSequence.toString().trim().toUpperCase())
                    ||entity.start_name.contains(charSequence.toString().trim().toUpperCase())
                    ||entity.des_name.contains(charSequence.toString().trim().toUpperCase())){
                list.add(entity);
            }
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onSearchCancel() {
        list.clear();
        list.addAll(orderList);
        adapter.notifyDataSetChanged();
    }
}
